using System;
using System.Collections;
using System.Collections.Generic;
using MyFrame.Mono;
using MyFrame.ResLoad;
using UnityEngine;
using UnityEngine.Events;


namespace MyFrame.Music
{

    public class MusicMgr : MonoBehaviour
    {
        private AudioSource bkMusic = null;

        private float bkValue = 1;

        public string path = "Music/";

        //音效
        private GameObject soundObj = null;
        private List<AudioSource> soundList = new List<AudioSource>();
        public float soundValue = 1;

        public MusicMgr()
        {
            MonoMgr.Instance.AddUpdateEvent(myUpdate);
        }

        private void myUpdate()
        {
            //播放完，手动停止音效
            for (int i = soundList.Count - 1; i >= 0; i--)
            {
                if (!soundList[i].isPlaying)
                {
                    Destroy(soundList[i]);
                    soundList.RemoveAt(i);
                }
            }
        }

        public void PlayBkMusic(string name)
        {
            if (bkMusic == null)
            {
                GameObject obj = new();
                obj.name = "BkMusic";
                bkMusic = obj.AddComponent<AudioSource>();
            }

            ResMgr.Instance.LoadAsync<AudioClip>(path + name, (clip) =>
            {
                bkMusic.clip = clip;
                bkMusic.loop = true;
                bkMusic.volume = bkValue;
                bkMusic.Play();
            });
        }

        public void ChagneBkValue(float value)
        {
            bkValue = value;
            if (bkMusic != null)
            {
                bkMusic.volume = bkValue;
            }
        }
        public void PauseBkMusic()
        {
            if (bkMusic != null)
            {
                bkMusic.Pause();
            }
        }
        public void StopBkMusic()
        {
            if (bkMusic != null)
            {
                bkMusic.Stop();
            }

        }

        public void PlaySound(string name, bool isLoop = false, UnityAction<AudioSource> callback = null)
        {
            if (soundObj == null)
            {
                soundObj = new GameObject("Sound");
            }

            ResMgr.Instance.LoadAsync<AudioClip>(path + name, (clip) =>
            {
                AudioSource audio = soundObj.AddComponent<AudioSource>();
                audio.clip = clip;
                audio.loop = isLoop;
                audio.volume = soundValue;
                audio.Play();
                soundList.Add(audio);
                callback?.Invoke(audio);
            });
        }

        public void StopSound(AudioSource audioSource)
        {
            if (soundList.Contains(audioSource))
            {
                soundList.Remove(audioSource);
                audioSource.Stop();
                Destroy(audioSource);
            }
        }

        public void ChangeSoundValue(float value)
        {
            soundValue = value;
            foreach (var item in soundList)
            {
                item.volume = soundValue;
            }
        }

    }
}
